package com.fandy.struts.SqlAccess;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerException;

import java.sql.*;

public class SqlServerHelper {
	
	private Connection con = null;
	private Statement stm = null;
	
	public SqlServerHelper()  
	{	
	
		SQLServerDataSource ds = new SQLServerDataSource();
		ds.setIntegratedSecurity(true);
		ds.setServerName("localhost");
		ds.setPortNumber(9553); 
		ds.setDatabaseName("Goods");
		
		try {
			
			con = ds.getConnection();
			stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			
		} catch (SQLServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public ResultSet executeScalar(String sql )  
	{		 
		
		ResultSet rs = null;
		try {
			
			rs = stm.executeQuery(sql);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		
		return rs; 
		
	}
	
	
	public int executeNoQuery(String sql) throws SQLException
	{
		int num = -1;
		
		try {
			if(!con.isClosed())
			{
				num = stm.executeUpdate(sql);
			}
			else
			{
				 
				
			}
		} 
		catch (SQLException e) {
			// TODO Auto-generated catch block
		
			e.printStackTrace();
		
		}
		finally
		{
			con.close();				
		}
		
		
		return num;
		
	}
	
	
}
